package com.frxuexi.park.view

import android.graphics.Color
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.base.entity.ParkBeanItem
import com.frxuexi.base.http.Const
import com.frxuexi.base.mvi.BaseActivity
import com.frxuexi.base.mvi.RequestType
import com.frxuexi.base.mvi.UiState
import com.frxuexi.park.ParkIntent
import com.frxuexi.park.ParkViewModel
import com.frxuexi.park.R
import com.frxuexi.park.databinding.ActivityParkAddBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import okhttp3.internal.notifyAll

@Route(path = "/park/ParkAddActivity")
@AndroidEntryPoint
class ParkAddActivity : BaseActivity<ActivityParkAddBinding, ParkIntent, ParkViewModel>() {
    var area="A区停车场"
    var areaId=1
    var type="标准车位"
    var status="空闲"
    val areaList = mutableListOf<String>("A区停车场", "B区停车场", "C区停车场")
    val typeList = mutableListOf<String>("标准车位", "VIP车位")

    @Autowired
    lateinit var pid:String
    override fun getVM(): ParkViewModel {
        return viewModels<ParkViewModel>().value
    }

    override fun getBind(): ActivityParkAddBinding {
        return ActivityParkAddBinding.inflate(layoutInflater)
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.ADD->{
                                ToastUtils.showLong("添加成功")
                                finish()
                            }
                            RequestType.UPDATE->{
                                ToastUtils.showLong("修改成功")
                                finish()
                            }
                            RequestType.DEFAULT->{//根据id查询详情
                                val item = it.data as ParkBeanItem
                                binding.parkaddCarNo.setText(item.spaceNumber)
                                binding.parkaddUser.setText(item.userName)
                                binding.parkaddCarNum.setText(item.licensePlate)
                                binding.parkaddRemark.setText(item.remarks)
                                binding.parkaddNum.setText("${item.remarks.length}/10")
                                if(!areaList.contains(item.areaName)){
                                    areaList.add(0,item.areaName)
                                    area=item.areaName
                                    val areaAdapter = ArrayAdapter(this@ParkAddActivity, android.R.layout.simple_spinner_item, areaList)
                                    binding.parkaddArea.adapter=areaAdapter
                                    binding.parkaddArea.adapter=areaAdapter
                                }

                                if(!typeList.contains(item.spaceType)){
                                    typeList.add(0,item.spaceType)
                                    type=item.spaceType
                                    val typeAdapter = ArrayAdapter(this@ParkAddActivity, android.R.layout.simple_spinner_item, typeList)
                                    typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                                    binding.parkaddType.adapter=typeAdapter
                                }
                                when(item.spaceStatus){
                                    "空闲"->binding.parkaddRg.check(R.id.rb_kx)
                                    "已占用"->binding.parkaddRg.check(R.id.rb_zy)
                                    "维护中"->binding.parkaddRg.check(R.id.rb_wh)
                                }
                            }

                            else -> {}
                        }

                    }

                    else -> {}
                }
            }
        }

    }

    override fun initView() {
        ARouter.getInstance().inject(this)

        if(!pid.isNullOrEmpty()){//修改
            vm.sendIntent(ParkIntent.getParkById(pid.toInt()))
        }

        binding.headParkAdd.apply {
            headBack.setOnClickListener {
                finish()
            }
            headTitle.setText("编辑车位")
            headAdd.setImageResource(com.frxuexi.base.R.drawable.ic_action_ok)
            headAdd.setOnClickListener {
                val carNum = binding.parkaddCarNum.text.toString()
                val remark = binding.parkaddRemark.text.toString()
                val carNo = binding.parkaddCarNo.text.toString()
                val item = ParkBeanItem(
                    areaId,
                    area,
                    null,
                    carNum,
                    remark,
                    carNo,
                    status,
                    type,
                    SPUtils.getInstance().getInt(Const.UID),
                    SPUtils.getInstance().getString(Const.UNAME),0
                )
                if(!pid.isNullOrEmpty()) {//修改
                    vm.sendIntent(ParkIntent.updatePark(pid.toInt(),item))
                }else {
                    vm.sendIntent(ParkIntent.addPark(item))
                }
            }
        }

        binding.parkaddRemark.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                val length = s?.length
                binding.parkaddNum.apply {
                    setText("${length}/10")
                    if(length==10){
                        setTextColor(Color.RED)
                    }else{
                        setTextColor(Color.DKGRAY)
                    }
                }

            }

            override fun afterTextChanged(s: Editable?) {
            }

        })



        val areaAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, areaList)
        areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.parkaddArea.adapter=areaAdapter
        binding.parkaddArea.onItemSelectedListener=object :OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                area=areaList[position]
                areaId=position+1
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }
        }


        val typeAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, typeList)
        typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.parkaddType.adapter=typeAdapter
        binding.parkaddType.onItemSelectedListener=object :OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                type=typeList[position]
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }
        }

        binding.parkaddRg.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId){
                R.id.rb_kx ->status="空闲"
                R.id.rb_zy ->status="已占用"
                R.id.rb_wh ->status="维护中"

            }
        }
    }



}